//
//  ThirdTableViewCell.m
//  UI_19_数据请求model嵌套
//
//  Created by li cheng on 16/8/12.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ThirdTableViewCell.h"
#import "ThirdCollectionViewCell.h"
#import "FindCarTools.h"
@interface ThirdTableViewCell()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) ThirdCollectionViewCell *thirdCollectionViewCell;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray *metalistMutableArray;
@end
@implementation ThirdTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_collectionView];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[ThirdCollectionViewCell class] forCellWithReuseIdentifier:@"ThirdCollectionViewCell"];
        [self handleData];
    }
    return self;
}
#pragma mark - 布局
- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat widthOfCollectionView = self.contentView.bounds.size.width;
    CGFloat height = self.contentView.bounds.size.height;
    _collectionView.frame = CGRectMake(0, 0, widthOfCollectionView, height);
}
#pragma mark - 数据解析
- (void)handleData{
    _metalistMutableArray = [NSMutableArray array];
    NSURLSession *urlSession = [NSURLSession sharedSession];
    NSURL *url = [NSURL URLWithString:@"http://cars.app.autohome.com.cn/cars_v5.8.0/cars/getmarks-a2-pm1.json"];
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
    urlRequest.HTTPMethod = @"GET";
    NSURLSessionDataTask *dataTask = [urlSession dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSError *error = nil;
            NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
            NSArray *metalistArray = dictionary[@"result"][@"metalist"];
            for (NSDictionary *dictionaryInMetalistArray in metalistArray) {
                NSString *string = dictionaryInMetalistArray[@"description"];
                if (string isEqualToString:@"标签") {
//                    NSArray *dictionaryInMetalistArray
                }
               FindCarTools *findCarTools = [FindCarTools findCarToolsWithDictionary:dictionaryInMetalistArray];
                [_metalistMutableArray addObject:findCarTools];
NSLog(@"——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————%@", findCarTools);
            }
            [self.collectionView reloadData];
        });
    }];
    [dataTask resume];
}
#pragma mark - 代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _metalistMutableArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ThirdCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ThirdCollectionViewCell" forIndexPath:indexPath];
    FindCarTools *findCarTools = _metalistMutableArray[indexPath.row];
    [cell.myButton setTitle:findCarTools.name forState:UIControlStateNormal];
    return cell;
}
#pragma makr 每一个item的布局
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout heightForItemAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"%ld", indexPath.row);
//    NSString *string = _buttonTitleArray[indexPath.row];
//    NSLog(@"%@", string);
//    CGRect newRect =  [string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
//    NSLog(@"+++++++++++++++%f", newRect.size.height);
//    return newRect.size.width;
//}
#pragma mark - 其他
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
